﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using System.ServiceModel;
using DataContracts;

namespace TrainingWorkshop.MVPWebsite.Authority.Views
{
    public class AuthorityListPresenter : Presenter<IAuthorityListView>
    {
        /// <summary>
        /// Authority Controller
        /// </summary>
        private IAuthorityController _controller;
        public AuthorityListPresenter([CreateNew] IAuthorityController controller)
        {
            _controller = controller;
        }
        /// <summary>
        /// Get All Authority
        /// </summary>
        /// <returns></returns>
        public void GetAllAuthority()
        {
            View.AuthorityDetailsList = _controller.GetAllAuthority();
        }

        /// <summary>
        /// Delete Authority
        /// </summary>
        /// <returns></returns>
        public void DeleteAuthority()
        {
            try
            {
                _controller.DeleteAuthority(View.AuthorityId);
                //View.ShowOperationMessage("Delete successfully!");
            }
            catch (FaultException ex)
            {
                View.ShowOperationMessage(ex.Message);
            }
        }

        public void UpdateCheck(int authorityId)
        {
            try
            {
                if (_controller.GetAuthorityById(authorityId) != null)
                {
                    View.Redirect("UpdateAuthority.aspx?id=" + authorityId);
                }
            }
            catch(FaultException ex)
            {
                View.ShowOperationMessage(ex.Message);
            }
        }

        /// <summary>
        /// Get Search Authority
        /// </summary>
        /// <param name="authorityName"></param>
        /// <param name="levelName"></param>
        /// <returns></returns>
        public void SearchAuthority()
        {
            DataContracts.AuthorityDTO authorityDTO = new AuthorityDTO();
            authorityDTO.AuthorityName = View.AuthorityName;
            DataContracts.AuthorityLevelDTO levelDTO = new AuthorityLevelDTO();
            levelDTO.LevelId = View.LevelId;
            authorityDTO.Level = levelDTO;
            if (authorityDTO.AuthorityName == "" && authorityDTO.Level.LevelId == 0)
            {
                GetAllAuthority();
            }
            else
            {
                List<AuthorityDTO> list = _controller.SearchAuthority(authorityDTO.AuthorityName, authorityDTO.Level.LevelId);
                if (list.Count == 0)
                {
                    View.ShowOperationMessage("Sorry!Do not have matching authority.");
                    View.SearchAuthorityList = list;
                }
                else
                {
                    View.SearchAuthorityList = list;
                } 
            }

        }

        /// <summary>
        /// Get All Authority Level
        /// </summary>
        /// <returns></returns>
        public void GetAllAuthorityLevel()
        {
            View.AuthorityLevelList = _controller.GetAllAuthorityLevel();
        }

        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
            GetAllAuthorityLevel();
            GetAllAuthority();
        }
    }
}




